package net.sxnic.item.mdata.action;

import net.sxnic.ugr.context.AppContext;
import net.sxnic.workflow.task.Task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class Delete extends MdataAction{

	private Logger logger = LoggerFactory.getLogger(Delete.class);
	
	public String execute() throws Exception{
		
		try {
			//只有自己才能删除
			Task task = taskManager.get(taskId);
			if(!task.getAssignee().equals(AppContext.getUserName(request))){
				msg ="deleteError";
				logger.info(AppContext.getUserName(request)+"=====非任务承担者试图删除相关实体！====="+task.getId());
				return INPUT;
			}
			
			if(task.isActived()){
				//只有当前任务未结束时才能删除
				
				String tempTitle = task.getMdata().getAppName();
				String id = task.getMdata().getId();			
				
				//删除task			
				taskManager.deleteById(taskId);
				
				//删除子表	和主表
				mdManager.deleteMdataById(id);
				
				logger.debug("用户:{} 删除实体:{} 成功！",AppContext.getUserName(request),tempTitle);
				
				msg ="deleteSuccess";
			}else{
				msg ="deleteError";
				logger.info(AppContext.getUserName(request)+"=====非激活任务状态试图删除相关实体！====="+task.getId());
				return INPUT;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
}
